/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "render_attr.hh"
#include "event_mng.hh"
#include <string.h>

CLASS_INIT(render_attr); // RENDER_ATTR CLASS

render_attr::render_attr()
{
    CLASS_CTOR(render_attr);
    ctor();
}

render_attr::~render_attr()
{
    CLASS_DTOR(render_attr);
    dtor();
}

void render_attr::ctor()
{
}

void render_attr::dtor()
{
}

bool render_attr::init()
{
    return true;
}

CLASS_INIT(render_color_anim); // RENDER_COLOR_ANIM CLASS

render_color_anim::render_color_anim()
{
    CLASS_CTOR(render_color_anim);
    ctor();    
}

render_color_anim::~render_color_anim()
{
    CLASS_DTOR(render_color_anim);
    dtor();
}

void render_color_anim::ctor()
{
    memset(color, 0, IS_RGBA*sizeof(unsigned char));
    memset(color_dest, 0, IS_RGBA*sizeof(unsigned char));
    start_time=event_mng::time_now();
    duration=-1;
}

void render_color_anim::dtor()
{
}

void render_color_anim::set_color(unsigned char c[IS_RGBA])
{
    memcpy(color, c, IS_RGBA*sizeof(unsigned char));
    duration=-1;
}

void render_color_anim::set_color(float c_red, float c_green, float c_blue, float c_alpha)
{
    color[IS_RED]  =RA_CONVERT_TO_UCHAR(c_red);
    color[IS_GREEN]=RA_CONVERT_TO_UCHAR(c_green);
    color[IS_BLUE] =RA_CONVERT_TO_UCHAR(c_blue);
    color[IS_ALPHA]=RA_CONVERT_TO_UCHAR(c_alpha);
    duration=-1;
}

void render_color_anim::set_color_dest(unsigned char c[IS_RGBA], int duration_usec)
{
    refresh(color);
    memcpy(color_dest, c, IS_RGBA*sizeof(unsigned char));
    duration=duration_usec;
    start_time=event_mng::time_now();
}

void render_color_anim::set_color_dest(float c_red, float c_green, float c_blue, float c_alpha, int duration_usec)
{
    refresh(color);
    color_dest[IS_RED]  =RA_CONVERT_TO_UCHAR(c_red);
    color_dest[IS_GREEN]=RA_CONVERT_TO_UCHAR(c_green);
    color_dest[IS_BLUE] =RA_CONVERT_TO_UCHAR(c_blue);
    color_dest[IS_ALPHA]=RA_CONVERT_TO_UCHAR(c_alpha);
    duration=duration_usec;
    start_time=event_mng::time_now();
}

bool render_color_anim::refresh(float result[IS_RGBA])
{
    if(duration<0)
    {
	for(int i=0; i<IS_RGBA; i++) result[i]=RA_CONVERT_TO_FLOAT(color[i]);
	return false;
    }

    int diff=event_mng::time_now()-start_time;
    if(diff>=duration)
    {
	memcpy(color, color_dest, IS_RGBA*sizeof(unsigned char));
	for(int i=0; i<IS_RGBA; i++) result[i]=RA_CONVERT_TO_FLOAT(color_dest[i]);
	duration=-1;
	return false;
    }

    // take progress percentage
    float prog=(float)diff/(float)duration;
    float iprog=1.0-prog;
    for(int i=0; i<IS_RGBA; i++)
	result[i]=(prog*(float)color_dest[i]+iprog*(float)color[i])*(1.0/255.0);
    return true;
}

bool render_color_anim::refresh(unsigned char result[IS_RGBA])
{
    if(duration<0)
    {
	memcpy(result, color, IS_RGBA*sizeof(unsigned char));
	return false;
    }

    int diff=event_mng::time_now()-start_time;
    if(diff>=duration)
    {
	memcpy(color, color_dest, IS_RGBA*sizeof(unsigned char));
	memcpy(result, color_dest, IS_RGBA*sizeof(unsigned char));
	duration=-1;
	return false;
    }

    // take progress percentage
    float prog=(float)diff/(float)duration;
    float iprog=1.0-prog;
    for(int i=0; i<IS_RGBA; i++)
	result[i]=(unsigned char)(prog*(float)color_dest[i]+iprog*(float)color[i]);
    return true;
}
